﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.KTTSCD
{
    public partial class FormTangTaiSan : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        private string idtam = "";
        public FormTangTaiSan()
        {
            InitializeComponent();
        }
        #region Event
        private void FormTangTaiSan_Load(object sender, EventArgs e)
        {
            GetListPhieuTangTaiSan();
        }

        void GetListPhieuTangTaiSan()
        {
            dgvChungTu.DataSource = db.ChungTus.Where(t => t.TrangThai == 3).ToList();
            dtiNgayChungTu.Value = DateTime.Now;
            dgvChungTu.Refresh();
            FilldgvChungTu();
            if (dgvChungTu.Rows.Count > 0)
            {
                var row = dgvChungTu.Rows[0];
                var tt = row.Cells["Id"].Value.ToString();
                dgvChungTuChiTiet.DataSource = db.ChungTuChiTiets.Where(t => t.ChungTuId == tt).ToList();

            }
            Reset();
            KiemTra();
        }

        private void bttThemMoi_Click(object sender, EventArgs e)
        {
            var f = new FormTangTaiSanThemCT();
            f.ShowDialog();
        }

        private void bttChinhSua_Click(object sender, EventArgs e)
        {
            var f = new FormTangTaiSanChinhSua {idct = idtam};
            f.ShowDialog();
        }

        private void bttXoa_Click(object sender, EventArgs e)
        {
            Delete();
            GetListPhieuTangTaiSan();
        }

        private void bttLamMoi_Click(object sender, EventArgs e)
        {
            GetListPhieuTangTaiSan();
            dtiNgayChungTu.Value = DateTime.Now;
        }

        private void bttTimKiem_Click(object sender, EventArgs e)
        {
            TimKiem();
            Reset();
            KiemTra();
        }
        private void dgvChungTu_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var index = e.RowIndex;
            var row = dgvChungTu.Rows[index];
            idtam = row.Cells["Id"].Value.ToString();
            KiemTra();
            GetChungTuChiTiet();
        }

        private void dgvChungTu_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var index = e.RowIndex;
            var row = dgvChungTu.Rows[index];
            idtam = row.Cells["Id"].Value.ToString();
            KiemTra();
            var f = new FormTangTaiSanChinhSua{ idct = idtam };
            f.ShowDialog();
        }

        private void dgvChungTu_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var index = e.RowIndex;
            var row = dgvChungTu.Rows[index];
            idtam = row.Cells["Id"].Value.ToString();
            KiemTra();
            var f = new FormTangTaiSanChinhSua { idct = idtam };
            f.ShowDialog();
        }

        private void dgvChungTu_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right) return;
            var ht = dgvChungTu.HitTest(e.X, e.Y);
            var rowindex = ht.RowIndex;
            if (rowindex == -1) return;
            var row = dgvChungTu.Rows[rowindex];
            idtam = row.Cells["Id"].Value.ToString();
            if (ht.ColumnIndex != -1 && ht.RowIndex != -1)
            {
                var Menu = new ContextMenuStrip();
                var lammoi = new ToolStripMenuItem("Làm mới");
                lammoi.MouseDown += bttLamMoi_Click;
                var edit = new ToolStripMenuItem("Chỉnh sửa");
                edit.MouseDown += bttChinhSua_Click;
                var xoa = new ToolStripMenuItem("Xóa");
                xoa.MouseDown += bttXoa_Click;
                Menu.Items.AddRange(new ToolStripItem[] { lammoi });
                Menu.Items.AddRange(new ToolStripItem[] { edit });
                Menu.Items.AddRange(new ToolStripItem[] { xoa });
                dgvChungTu.ContextMenuStrip = Menu;
            }
            else
                dgvChungTu.ContextMenuStrip = null;
            KiemTra();
        }

        private void dgvChungTu_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var index = e.RowIndex;
            var row = dgvChungTu.Rows[index];
            idtam = row.Cells["Id"].Value.ToString();
            KiemTra();
            GetChungTuChiTiet();
        }
        #endregion Event
        #region Metho
        void Delete()
        {
            if (idtam != "")
            {
                var t = db.ChungTus.Find(idtam);
                if (
                    MessageBox.Show("Bạn muốn xóa chứng từ: " + t.Id + "!", "Xác nhận", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        db.ChungTus.Remove(t);
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }

                }
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa!");
            }
        }
        void Reset()
        {
            idtam = "";
            bttChinhSua.Enabled = false;
            bttXoa.Enabled = false;
        }
        void KiemTra()
        {
            if (idtam != "")
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }

        void TimKiem()
        {
            FilldgvChungTu();
            var iddt = Convert.ToInt32(cbbDoiTac.SelectedValue);
            var idnv = Convert.ToInt32(cbbNhanVien.SelectedValue);
            var nct = Convert.ToDateTime(dtiNgayChungTu.Value);
            if (iddt != 0 && idnv != 0)
            {
                dgvChungTu.DataSource =
                db.ChungTus.Where(t => t.NgayChungTu == nct && t.DoiTacId == iddt && t.NhanVienId == idnv && t.TrangThai == 3);
            }
            else if (iddt == 0 && idnv != 0)
            {
                dgvChungTu.DataSource =
                db.ChungTus.Where(t => t.NgayChungTu == nct && t.NhanVienId == idnv && t.TrangThai ==3 );
            }
            else if (iddt != 0 && idnv == 0)
            {
                dgvChungTu.DataSource =
                db.ChungTus.Where(t => t.NgayChungTu == nct && t.DoiTacId == iddt && t.TrangThai ==3 );
            }
            else
            {
                dgvChungTu.DataSource =
                db.ChungTus.Where(t => t.NgayChungTu == nct && t.TrangThai == 3 );
            }
            dgvChungTu.Refresh();
            FilldgvChungTuChiTiet();
            var row = dgvChungTu.Rows[0];
            var tt = row.Cells["Id"].Value.ToString();
            dgvChungTuChiTiet.DataSource = db.ChungTuChiTiets.Where(t => t.ChungTuId == tt).ToList();
            Reset();
            KiemTra();
        }
        void FilldgvChungTu()
        {
            var n = dgvChungTu.Columns["DoiTacId"] as DataGridViewComboBoxColumn;
            if (n != null)
            {
                n.DataSource = db.DoiTacs.ToList();
                n.DisplayMember = "TenDoiTac";
                n.ValueMember = "Id";
            }

            var nv = dgvChungTu.Columns["NhanVienId"] as DataGridViewComboBoxColumn;
            if (nv == null) return;
            nv.DataSource = db.NhanViens.ToList();
            nv.DisplayMember = "TenNhanVien";
            nv.ValueMember = "Id";
        }

        void FilldgvChungTuChiTiet()
        {
            var n = dgvChungTuChiTiet.Columns["TaiKhoanNo"] as DataGridViewComboBoxColumn;
            if (n != null)
            {
                n.DataSource = db.TaiKhoans.ToList();
                n.DisplayMember = "Id";
                n.ValueMember = "Id";
            }

            var c = dgvChungTuChiTiet.Columns["TaiKhoanCo"] as DataGridViewComboBoxColumn;
            if (c != null)
            {
                c.DataSource = db.TaiKhoans.ToList();
                c.DisplayMember = "Id";
                c.ValueMember = "Id";
            }

            var h = dgvChungTuChiTiet.Columns["TaiSanId"] as DataGridViewComboBoxColumn;
            if (h != null)
            {
                h.DataSource = db.TaiSans.ToList();
                h.DisplayMember = "TenTaiSan";
                h.ValueMember = "Id";
            }

            var t = dgvChungTuChiTiet.Columns["NhomThueHangHoaId"] as DataGridViewComboBoxColumn;
            if (t != null)
            {
                t.DataSource = db.NhomThueHangHoas.Where(tt => tt.LoaiThue == 2).ToList();
                t.DisplayMember = "TenThue";
                t.ValueMember = "Id";
            }

            var k = dgvChungTuChiTiet.Columns["PhongBanId"] as DataGridViewComboBoxColumn;
            if (k == null) return;
            k.DataSource = db.PhongBans.ToList();
            k.DisplayMember = "TenPhongBan";
            k.ValueMember = "Id";
        }
        void GetChungTuChiTiet()
        {
            FilldgvChungTuChiTiet();
            if (idtam == "") return;
            dgvChungTuChiTiet.DataSource = db.ChungTuChiTiets.Where(t => t.ChungTuId == idtam).ToList();
            dgvChungTuChiTiet.Refresh();
        }
        #endregion Metho

        
    }
}